<?php

/**
 * This is for searching...
 */
class Validations_Search {

    public function doSearch($searchCriteria) {
        $db = new Ads_Model_Rentdetails();
        $dbData = $db->fetchAll();
        $searchArray = array();
        if (sizeof($dbData) == 0) {
            return $searchArray;
        }
        foreach ($dbData as $property) {
            $decodeJson = json_decode($property['details']);
            $cityMatched = false;
            $stateMatched = false;
            $zipcodeMatched = false;
            $propertyTypeMatched = false;
            $rentalPeiodMatched = false;
            $finalMatchDec = false;
            $furnishTypeMatched = false;
            $roomMatched = false;
            $areaMatched = false;
            $rentMatched = false;
            $fncount = 0;

            #1 city compare
            if ($searchCriteria['city'] == -1) {
                $cityMatched = true;
                $fncount++;
            } else if ($searchCriteria['city'] == $db->getCityIdOfPropertyJson($decodeJson)) {
                $cityMatched = true;
            }


            #2 state compare
            if ($searchCriteria['state'] == -1) {
                $stateMatched = true;
                $fncount++;
            } else if ($searchCriteria['state'] == $db->getStateIdofPropertyJson($decodeJson)) {
                $stateMatched = true;
            }

            #3 zipcode compare
            if ($searchCriteria['zipcode'] == -1) {
                $zipcodeMatched = true;
                $fncount++;
            } else if ($searchCriteria['zipcode'] == $db->getZipIdOfPropertyJson($decodeJson)) {
                $zipcodeMatched = true;
            }

            #4 property type compare
            if ($searchCriteria['propertyType'] == -1) {
                $propertyTypeMatched = true;
                $fncount++;
            } else if ($searchCriteria['propertyType'] == $db->getProprtyTypeIdofPropertyJson($decodeJson)) {
                $propertyTypeMatched = true;
            }

            #5 rental period compare
            if ($searchCriteria['rentalPeriod'] == -1) {
                $rentalPeiodMatched = true;
                $fncount++;
            } else if ($searchCriteria['rentalPeriod'] == $db->getRentalPeriodIdOfPropertyJson($decodeJson)) {
                $rentalPeiodMatched = true;
            }

            #6 furnish type compare
            if ($searchCriteria['furnishType'] == -1) {
                $furnishTypeMatched = true;
                $fncount++;
            } else if ($searchCriteria['furnishType'] == $db->getFurnishTypeIdOfPropertyJson($decodeJson)) {
                $furnishTypeMatched = true;
            }

            #7 room compare
            $roomsInData = $db->getRoomOfPropertyJson($decodeJson);
            $searchingRooms = $searchCriteria['rooms'];
            if ($searchingRooms == 0) {
                $roomMatched = true;
                $fncount++;
            } else if ($searchingRooms == $roomsInData) {
                $roomMatched = true;
            } else if ($searchingRooms == 10 && $searchingRooms <= $roomsInData) {
                $roomMatched = true;
            }

            #8 area compare 1=>1-50, 2=>51-100, 3=>101-150, 4=>151-200, 5=>201-250,6=>251+
            $searchingArea = $searchCriteria['area'];
            $areaInData = $db->getAreaOfPropertyJson($decodeJson);
            switch ($searchingArea) {
                case 0:
                    $areaMatched = true;
                    $fncount++;
                    break;
                case 1:
                    $areaMatched = $this->isBetween($areaInData, 1, 50);
                    break;
                case 2:
                    $areaMatched = $this->isBetween($areaInData, 51, 100);
                    break;
                case 3:
                    $areaMatched = $this->isBetween($areaInData, 101, 150);
                    break;
                case 4:
                    $areaMatched = $this->isBetween($areaInData, 151, 200);
                    break;
                case 5:
                    $areaMatched = $this->isBetween($areaInData, 201, 250);
                    break;
                case 6:
                    $areaMatched = ($areaInData >= 251) ? true : false;
                default:
                    break;
            }

            #9 rent compare 1=>1-500, 2=>501-1000, 3=>1001-1500, 4=>1501-2000, 5=>2001-2500,6=>2501+
            $searchingRent = $searchCriteria['rent'];
            $rentInData = $db->getRentOfPropertyJson($decodeJson);
            switch ($searchingRent) {
                case 0:
                    $rentMatched = true;
                    $fncount++;
                    break;
                case 1:
                    $rentMatched = $this->isBetween($rentInData, 1, 500);
                    break;
                case 2:
                    $rentMatched = $this->isBetween($rentInData, 501, 1000);
                    break;
                case 3:
                    $rentMatched = $this->isBetween($rentInData, 1001, 1500);
                    break;
                case 4:
                    $rentMatched = $this->isBetween($rentInData, 1501, 2000);
                    break;
                case 5:
                    $rentMatched = $this->isBetween($rentInData, 2001, 2500);
                    break;
                case 6:
                    $rentMatched = ($rentInData >= 2501) ? true : false;
                default:
                    break;
            }

            #final dec            
            if ($fncount == 9) {
                $finalMatchDec = false;
            } else {
                $finalMatchDec = true;
            }

            //check for all
            if ($cityMatched && $stateMatched && $zipcodeMatched && $propertyTypeMatched && $rentalPeiodMatched && $furnishTypeMatched && $roomMatched && $areaMatched && $rentMatched && $finalMatchDec) {
                array_push($searchArray, $property);
            }
        }
        return $searchArray;
    }

    /**
     * 
     * @param type $compareValue
     * @param type $value1
     * @param type $value2
     */
    public function isBetween($compareValue, $value1, $value2) {
        $isBetween = false;
        if ($compareValue >= $value1 && $compareValue <= $value2) {
            $isBetween = true;
        }
        return $isBetween;
    }

    public function mapSearch($id) {
        $rentDetailsDb = new Ads_Model_Rentdetails();
        $searchArray = array();
        $details = $rentDetailsDb->fetchAll();
        foreach ($details as $property) {            
            $djson = json_decode($property['details']);
            $proDetails = $djson->details;
            if($proDetails->state == $id){
                array_push($searchArray, $property);
            }
        }
        return $searchArray;
    }

}